

#include <QtGui>
#include "MainWindow.h"

FlashCardGUI::FlashCardGUI(QWidget *parent)
    : QWidget(parent)
{
	// Initialize the Class
	// Sets various attributes for the objects such as buttons

	currentIndex = 0;
	
	// Create Buttons and Text Fieldss
	createButton = new QPushButton(tr("&Create"));
	createButton->show();
	addButton = new QPushButton(tr("&Add"));
	addButton->hide();
	cancelButton = new QPushButton(tr("&Cancel"));
	cancelButton->hide();
	nextButton = new QPushButton(tr("&Next"));
	nextButton->setEnabled(false);
	previousButton = new QPushButton(tr("&Previous"));
	previousButton->setEnabled(false);
	editButton = new QPushButton(tr("&Edit"));
	editButton->setEnabled(false);
	deleteButton = new QPushButton(tr("&Remove"));
	deleteButton->setEnabled(false);
	flipButton = new QPushButton(tr("&Flip"));
	flipButton->setEnabled(false);

	questionLabel = new QLabel(tr("Question:"));
	questionLabel->hide();
	answerLabel = new QLabel(tr("Answer:"));
	answerLabel->hide();
	
	// Text fields for creating new questions and answers
	cardQuestion = new QLineEdit;
	cardQuestion->setReadOnly(true);
	cardQuestion->hide();
	cardAnswer = new QLineEdit;
	cardAnswer->setReadOnly(true);
	cardAnswer->hide();

	// Text field to display current card
	displayCard = new QTextEdit;
	displayCard->setReadOnly(true);




	// Connect buttons to functions (Signals -> Slots)
	connect(createButton, SIGNAL(clicked()), this, SLOT(createCard()));
	connect(addButton, SIGNAL(clicked()), this, SLOT(addCard()));
	connect(cancelButton, SIGNAL(clicked()), this, SLOT(cancel()));
	connect(nextButton, SIGNAL(clicked()), this, SLOT(next()));
	connect(previousButton, SIGNAL(clicked()), this, SLOT(previous()));
	connect(editButton, SIGNAL(clicked()), this, SLOT(editCard()));
	connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteCard()));
	connect(flipButton, SIGNAL(clicked()), this, SLOT(flipCard()));


	// Set up the vertical box layout for the buttons
	QVBoxLayout *leftColumn = new QVBoxLayout;
	leftColumn->addWidget(createButton, Qt::AlignTop);
	leftColumn->addWidget(addButton);
	leftColumn->addWidget(cancelButton);
	leftColumn->addWidget(editButton);
	leftColumn->addWidget(deleteButton);
	leftColumn->addWidget(questionLabel);
	leftColumn->addWidget(cardQuestion);
	leftColumn->addWidget(answerLabel);
	leftColumn->addWidget(cardAnswer);
	leftColumn->addStretch();


	// Set up the horizontal box layout for the prev / next buttons
	QHBoxLayout *bottomRow = new QHBoxLayout;
	bottomRow->addWidget(previousButton);
	bottomRow->addWidget(flipButton);
	bottomRow->addWidget(nextButton);


	// Set up the grid layout to place all objects
	// Want to tweak this so that the button layout is even with top
	QGridLayout *mainLayout = new QGridLayout;
	mainLayout->addWidget(displayCard, 0, 1);
	mainLayout->addLayout(leftColumn, 0, 0);
	mainLayout->addLayout(bottomRow, 1, 1);

	setLayout(mainLayout);
	setWindowTitle(tr("Flash-Soft"));
}

// Button Function for Adding a Contact
void FlashCardGUI::createCard()
{
    
    cardQuestion->clear();
    cardAnswer->clear();

    updateInterface(AddingMode);
}

// Button function for submiting the contact to list
void FlashCardGUI::addCard()
{
    QString question = cardQuestion->text();
    QString answer = cardAnswer->text();
    
    if (question.isEmpty() || answer.isEmpty()) {
        QMessageBox::information(this, tr("Empty Field"),
            tr("Please enter a question and answer."));
        return;
    }
    // 
    if (currentMode == AddingMode || currentMode == EditingMode) {
		Card newCard(question.toUtf8().constData(), answer.toUtf8().constData());
		cardDeck.add(newCard);
	}
    
    updateInterface(NavigationMode);
}

// Button for canceling the addition of a new person
void FlashCardGUI::cancel()
{
	updateInterface(NavigationMode);
}

void FlashCardGUI::next()
{	
	if (currentIndex < cardDeck.length()) {
		currentIndex++;
	}
	if (currentIndex == cardDeck.length()) {
		currentIndex = 0;
	}
	Card currentCard = cardDeck.get(currentIndex);
	QString question = QString::fromStdString(currentCard.showQuestion());
	displayCard->setText(question);
	answerShowing = false;
}


void FlashCardGUI::previous()
{  
    
	if (currentIndex > 0) {
		currentIndex--;
	} else if (currentIndex == 0) {
		currentIndex = cardDeck.length() - 1;
	}
	Card currentCard = cardDeck.get(currentIndex);
	QString question = QString::fromStdString(currentCard.showQuestion());
	displayCard->setText(question);
	answerShowing = false;
}

void FlashCardGUI::editCard()
{
	// Update Fields
}

void FlashCardGUI::deleteCard()
{
    updateInterface(NavigationMode);
}


// Flips the current card
void FlashCardGUI::flipCard() {
	Card currentCard = cardDeck.get(currentIndex);
	QString answer = QString::fromStdString(currentCard.showAnswer());
	QString question = QString::fromStdString(currentCard.showQuestion());
	if (!answerShowing) {
		displayCard->setText(answer);
		answerShowing = true;
	} else {
		displayCard->setText(question);
		answerShowing = false;
	}
	
}

void FlashCardGUI::updateInterface(Mode mode)
{
	currentMode = mode;

	switch (currentMode) {

	case AddingMode:
	case EditingMode:
		cardQuestion->setReadOnly(false);
		cardQuestion->setFocus(Qt::OtherFocusReason);
		cardAnswer->setReadOnly(false);

		createButton->setEnabled(false);
		editButton->setEnabled(false);
		deleteButton->setEnabled(false);

		nextButton->setEnabled(false);
		previousButton->setEnabled(false);
		addButton->show();
		cancelButton->show();
		questionLabel->show();
		cardQuestion->show();
		answerLabel->show();
		cardAnswer->show();
		break;
	case NavigationMode:
		
		cardQuestion->hide();
		cardAnswer->hide();
		createButton->setEnabled(true);

		int number = cardDeck.length();
		editButton->setEnabled(number >= 1);
		deleteButton->setEnabled(number >= 1);
		nextButton->setEnabled(number > 1);
		previousButton->setEnabled(number > 1);
		flipButton->setEnabled(number > 1);

		addButton->hide();
		cancelButton->hide();
		
		questionLabel->hide();
		cardQuestion->hide();
		answerLabel->hide();
		cardAnswer->hide();
		break;
    }
}
